var swipe_fn = function( config, $swipe ){
	var defalut = [];
	if(config == undefined) {
		config = $.extend(defalut, config);
	}
	if($swipe.length  == 0 ){return;}
	var $swipelinner = $('.swipe-wrap', $swipe),
		$item = $('.item', $swipe),
		$swipe_tip = $('.tip_num', $swipe),
		$swipe_tip_title = $('.tip_title', $swipe);
	var bullets = $('.swipe-indicators li', $swipe),
		bullets_len = bullets.length,
		btn_page_prev = $('.btn-prev', $swipe),
		btn_page_next = $('.btn-next', $swipe);
		$('.t_totle', $swipe_tip).text(bullets_len);
		$swipe_tip_title.text(config[0].title);
		if(config.length>0){
			for(var i in config){
				$item.eq(i).attr('data-url', config[i].url);
			}
		}
	window.mySwipe = Swipe($swipe[0], {
		startSlide:0,
		auto: 3000,
		continuous: true,
		disableScroll: true,
		stopPropagation: true,
		callback: function(pos) {
			$('.t_num', $swipe_tip).text(pos+1);
			bullets.eq(pos).addClass('active').siblings().removeClass('active');
			$swipe_tip_title.text(config[pos].title);

		},
		transitionEnd: function(index, element) {}
	});
	
	$item.on('click', function(){
		var self = $(this);
		location.href = self.attr('data-url');
	});
	
	window.setTimeout(function(){
		$swipelinner.fadeIn(300);
	},100);
	btn_page_prev.on('click', function(){
		mySwipe.prev();
	});
	btn_page_next.on('click', function(){
		mySwipe.next();
	});
	bullets.on('click', function(){
		var self = $(this);
		var index = bullets.index(self);
		mySwipe.slide(index, 300);
	});
},
userAgent = navigator.userAgent.toLowerCase(),
browser={   
		version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],   
		safari: /webkit/.test( userAgent ),   
		opera: /opera/.test( userAgent ),   
		msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),   
		mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )   
};
// control the clicker and fix the smarty bug
function clickFnAjax(oData){
	var jQ_visits = $('.main-content .visits'),
		num = parseInt($.trim(jQ_visits.text())),
		clickXhr = '';
	if(clickXhr){
		clickXhr.abort();
	}
	clickXhr = $.ajax({
		type: "post",
		url: 'ajaxClick.php',
		data: oData,
		dataType: "json",
		success: function(msg){
			if(msg.status == 'successful'){
				jQ_visits.text(num + 1 + ' ');					
			} else if(msg.status == 'moreTimes'){
				//
			}
		}
	});
}
//extend
(function(){
	$.fn.extend({
		initialize:function(){
			
		},
		renderBody: function(){
			var self = this;
			self.on('vmousedown', '.fake', self.addFakeClass);
			self.on('vmouseup vmouseout vmousecancel  vmouseover', '.fake', self.removeFakeClass);
		},
		addFakeClass: function(e) {
			$(e.currentTarget).addClass('fake-active');
		},
		removeFakeClass: function(e) {
			$(e.currentTarget).removeClass('fake-active');
		}
	});
})(jQuery);

$(function(){
	$('body').renderBody();
	// footer function
	function footer_fn(){
		var $footnav = $('.footer-nav');
		$('body').on('taphold',  function(e){
			e.stopImmediatePropagation(); 
			$('.subnav', $footnav).hide();
			return false; 
		});
		$footnav.on('tap', 'li', function(e){
			e.stopPropagation();
		});
		$footnav.on('tap', 'li.item', function(){
			subnav_resize.call(this);
		});
		function subnav_resize(){
			var self = $(this),   self_width = self.outerWidth(),  $subnav = self.find('.subnav'), self_subnav_width = $subnav.outerWidth(), m_x = parseInt((self_width - self_subnav_width), 10)/2;
			if($subnav.is(':visible')){
				$subnav.stop(true, true).slideUp(300);
			} else {
				$subnav.css({'left': m_x}).stop(true, true).slideDown(300).parents('li').siblings().find('.subnav').hide();
			}
		}
		/*$(window).on('resize', function(){
			$('li.item', $footnav).each(function(){
				subnav_resize.call(this);
			});
		});*/
	}

	footer_fn();
	
	
	//click
	if($('.main-content .visits').length > 0){
		var href = location.search,
			pathname = location.pathname,
			oData = href.replace('?',''),
			action = '';	
		if( new RegExp('article_view.php').test(pathname) ){
			action = '&action=hits_articles';
			clickFnAjax(oData + action);	
		} else if( new RegExp('article_view_').test(pathname) ){
			var arr = pathname.split('_'),
				oData = "id=" + arr[2] +"&cid=''";
			action = '&action=hits_articles';
			clickFnAjax(oData + action);	
		}   else {
			//
		} 
	}
	var $gotop = $('.ui-gotop');
	if($gotop.length > 0){		
		$gotop.on('click', function(){
			$('html').animate({scrollTop:0}, 200);
			if (browser.safari) {
				$('body').animate({scrollTop:0}, 200);
			}
			
		});
	}
	
});
(function(){
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
		WeixinJSBridge.call('hideToolbar');
	});
	//$.mobile.pageLoading();
})();
// page init
$(document).on("pageinit",function(){
	$('.myfooter li').not('.ui-btn-active').on('tap',function(){
		$(this).children('a').addClass('ui-btn-active').parents('li').siblings().children('a').removeClass('ui-btn-active');
	});        
	               
});